<?php
/**
 * 注释不留名,代码随便用
 * Date: 2019/4/25
 * Time: 11:26
 */

namespace app\common\library\basicadmin\logic;


use app\common\controller\BasicAdmin;
use app\common\library\basicadmin\Logic;
use think\Model;

/**
 * 数据更新管理器
 * Class Save
 * @package library\logic
 */
class Save extends Logic
{
    /**
     * @var Model
     */
    protected $model;
    /**
     * 表单扩展数据
     * @var array
     */
    protected $data;
    /**
     * 表单额外更新条件
     * @var array
     */
    protected $where;

    /**
     * 数据对象主键名称
     * @var array|string
     */
    protected $pkField;

    /**
     * 数据对象主键值
     * @var string
     */
    protected $pkValue;

    /**
     * Save constructor.
     * @param Model $model 模型名称
     * @param array $data 表单扩展数据
     * @param string $pkField 数据对象主键
     * @param array $where 额外更新条件
     */
    public function __construct(Model $model, $data = [], $pkField = '', $where = [])
    {
        $this->where = $where;
        $this->model = $model;
        $this->data = empty($data) ? request()->post() : $data;
        $this->pkField = empty($pkField) ? $this->model->getPk() : $pkField;
        $this->pkValue = request()->post($this->pkField, null);
        parent::__construct();
    }

    /**
     * 逻辑器初始化
     * /**
     * @param BasicAdmin $controller
     * @return bool|mixed
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function init(BasicAdmin $controller)
    {
        $this->controller = $controller;
        $isUpdate = false;
        // 主键限制处理
        $db = $this->model->db();
        $data = array_merge($this->request->post(), $this->data);
        // 前置回调处理
        if (false === $this->controller->callback('_save_filter', $this->model, $data)) {
            return false;
        }
        // 执行更新操作
        if(!empty($this->where)) {
            $isUpdate = true;
            $db->where($this->where);
        }
        if ($this->pkValue !== null) {
            if(strpos($this->pkValue, ',')){
                $db->whereIn($this->pkField, explode(',',$this->pkValue));
            }else {
                $db->where($this->pkField, $this->pkValue);
            }
            if (isset($data)) unset($data[$this->pkField]);
            $isUpdate = true;
        }

        if(!$isUpdate){
            $result = $this->model->save($data);
        }else{
            $result = $db->update($this->fieldFilter($data));
        }
        // 结果回调处理
        if (false === $this->controller->callback('_save_result', $result, $data)) {
            return $result;
        }
        // 回复前端结果
        if ($result !== false) {
            $this->controller->success(lang('Successful Data Record Preservation'), '');
        } else {
            $this->controller->error(lang('Data save failed. Please try again later'));
        }
    }

    /**
     * 过滤字段
     */
    protected function fieldFilter($data){
        $fields = $this->model->getTableFields();
        foreach($data as $field => $val){
            if(!in_array($field, $fields)){
                unset($data[$field]);
            }
        }
        return $data;
    }

}